package com.tongtailian.www.stir.adapter;

import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.tongtailian.www.stir.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by liuml on 2020/9/6 16:48
 */
public class BluetoothDeviceAdapter extends BaseQuickAdapter<BleDevice, BaseViewHolder> {
    public BluetoothDeviceAdapter(int layoutResId, @Nullable List<BleDevice> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, BleDevice bleDevice) {


        if (bleDevice != null) {
            boolean isConnected = BleManager.getInstance().isConnected(bleDevice);
            String name = bleDevice.getName();
            String mac = bleDevice.getMac();
            int rssi = bleDevice.getRssi();
            if (TextUtils.isEmpty(name)) {
                name = bleDevice.getDevice().toString();
            }
            helper.setText(R.id.txt_name, (name))
                    .setText(R.id.txt_mac, mac)
                    .setText(R.id.txt_rssi, (String.valueOf(rssi)));

            helper.addOnClickListener(R.id.btn_connect)
                    .addOnClickListener(R.id.btn_disconnect)
                    .addOnClickListener(R.id.txt_name)
                    .addOnClickListener(R.id.btn_detail);

            if (isConnected) {
                helper.setImageResource(R.id.img_blue, R.mipmap.ic_blue_connected)
                        .setTextColor(R.id.txt_name, 0xFF1DE9B6)
                        .setTextColor(R.id.txt_mac, 0xFF1DE9B6)
                        .setVisible(R.id.layout_idle, false)
                        .setVisible(R.id.layout_connected, true);
            } else {
                helper.setImageResource(R.id.img_blue, R.mipmap.ic_blue_remote)
                        .setTextColor(R.id.txt_name, 0xFF000000)
                        .setTextColor(R.id.txt_mac, 0xFF000000)
                        .setVisible(R.id.layout_idle, true)
                        .setVisible(R.id.layout_connected, false);
            }

        }
    }
}